package com.icss.ah.productGroup.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.icss.ah.base.bean.EasyuiDataGridJson;
import com.icss.j2ee.util.UUID;

/**
 * 相似商品初始化功能服务类
 * @author xuyl
 * @date 2017-5-31
 */
@Service
@Transactional
public class ProductGroupService {
	
	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	
	private EasyuiDataGridJson dataGridJson = new EasyuiDataGridJson();
	
	/**
	 * 相似商品组列表查询
	 */
	public EasyuiDataGridJson queryProductGroupList(Map<String, String> map){
		try {
			List<Map<String, Object>> rows = sqlSessionTemplate.selectList("ProductGroupBean.queryProductGroupList",map);
			Long total = sqlSessionTemplate.selectOne("ProductGroupBean.queryProductGroupListCount",map);
			dataGridJson.setRows(rows);
			dataGridJson.setTotal(total);
			infolog.info("ProductGroupService|queryProductGroupList");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ProductGroupService|queryProductGroupList",e);
		}
		return dataGridJson;
	}
	
	/**
	 * 添加相似商品组
	 */
	public String saveProductGroupInfo(Map<String, String> map){
		String result = "保存失败";
		try {
			sqlSessionTemplate.insert("ProductGroupBean.saveProductGroupInfo",map);
			result = "保存成功";
			infolog.info("ProductGroupService|saveProductGroupInfo");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ProductGroupService|saveProductGroupInfo",e);
		}
		return result;
	}
	
	/**
	 * 分组信息查询
	 */
	public Map<String, Object> queryProductGroupInfo(String uuid){
		Map<String, Object> obj = new HashMap<String, Object>();
		try {
			obj = sqlSessionTemplate.selectOne("ProductGroupBean.queryProductGroupInfo",uuid);
			infolog.info("ProductGroupService|queryProductGroupInfo");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ProductGroupService|queryProductGroupInfo",e);
		}
		return obj;
	}
	
	/**
	 * 编辑商品组
	 */
	public String updateProductGroupInfo(Map<String, String> map){
		String result = "false";
		try {
			sqlSessionTemplate.update("ProductGroupBean.updateProductGroupInfo",map);
			result = "true";
			infolog.info("ProductGroupService|updateProductGroupInfo");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ProductGroupService|updateProductGroupInfo",e);
		}
		return result;
	} 
	
	/**
	 * 删除相似商品组
	 */
	public String delProductGroupInfo(String uuids){
		String result = "删除失败";
		try {
			if(uuids != null && uuids.length() > 0){
				String [] uuidArr = uuids.split(",");
				sqlSessionTemplate.delete("ProductGroupBean.delProductGroupInfo",uuidArr);
				sqlSessionTemplate.delete("ProductGroupBean.delGroupProduct",uuidArr);
				result = "删除成功";
			}else result = "请选择要删除的数据";
			infolog.info("ProductGroupService|delProductGroupInfo");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ProductGroupService|delProductGroupInfo",e);
		}
		return result;
	}
	
	/**
	 * 商品列表查询
	 */
	public EasyuiDataGridJson queryProductInfoList(Map<String, String> map){
		try {
			List<Map<String, Object>> rows = sqlSessionTemplate.selectList("ProductGroupBean.queryProductInfoList",map);
			Long total = sqlSessionTemplate.selectOne("ProductGroupBean.queryProductInfoListCount",map);
			dataGridJson.setRows(rows);
			dataGridJson.setTotal(total);
			infolog.info("ProductGroupService|queryProductInfoList");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ProductGroupService|queryProductInfoList",e);
		}
		return dataGridJson;
	}
	
	/**
	 * 相似商品组商品列表查询
	 */
	public EasyuiDataGridJson queryProductList(Map<String, String> map){
		try {
			List<Map<String, Object>> rows = sqlSessionTemplate.selectList("ProductGroupBean.queryProductList",map);
			Long total = sqlSessionTemplate.selectOne("ProductGroupBean.queryProductListCount",map);
			dataGridJson.setRows(rows);
			dataGridJson.setTotal(total);
			infolog.info("ProductGroupService|queryProductList");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ProductGroupService|queryProductList",e);
		}
		return dataGridJson;
	}
	
	/**
	 * 添加相似商品
	 */
	public String saveProductInfo(Map<String, String> map,String uuids,String groupUuid){
		String result = "添加失败";
		try {
			String [] uuidArr = uuids.split(",");
			List<Map<String, String>> list = new ArrayList<Map<String,String>>();
			for(String uuid : uuidArr){
				Map<String, String> m = new HashMap<String, String>();
				m.put("uuid", new UUID().toString());
				m.put("productId", uuid);
				m.put("status", "0");
				m.put("sortNum", "1");
				m.put("groupUuid", groupUuid);
				m.put("proGroupName", "");
				m.put("corpId", map.get("corpId"));
				m.put("shopId", map.get("shopId"));
				m.put("userCode", map.get("userCode"));
				list.add(m);
			}
			sqlSessionTemplate.insert("ProductGroupBean.saveProductInfo",list);
			result = "添加成功";
			infolog.info("ProductGroupService|saveProductInfo");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ProductGroupService|saveProductInfo",e);
		}
		return result;
	}
	
	/**
	 * 调整商品顺序
	 */
	public String updateProduct(Map<String, String> map){
		String result = "保存失败";
		try {
			String uuids = map.get("uuid");
			String [] uuidArr = uuids.split(",");
			if(uuidArr.length > 1){
				String userCode = map.get("userCode");
				String status = map.get("status");
				List<Map<String, String>> list = new ArrayList<Map<String,String>>();
				for(String uuid : uuidArr){
					Map<String, String> obj = new HashMap<String, String>();
					obj.put("uuid", uuid);
					obj.put("userCode", userCode);
					obj.put("status", status);
					list.add(obj);
					sqlSessionTemplate.update("ProductGroupBean.updateProduct",obj);
				}
//				sqlSessionTemplate.update("ProductGroupBean.updateProductStatus",list);
			}else{
				sqlSessionTemplate.update("ProductGroupBean.updateProduct",map);
			}
			result = "保存成功";
			infolog.info("ProductGroupService|updateProduct");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ProductGroupService|updateProduct",e);
		}
		return result;
	}
	
	/**
	 * 删除相似商品
	 */
	public String delProduct(String uuids){
		String result = "删除失败";
		try {
			if(uuids != null && uuids.length() > 0){
				String [] uuidArr = uuids.split(",");
				sqlSessionTemplate.delete("ProductGroupBean.delProduct",uuidArr);
				result = "删除成功";
			}else result = "请选择要删除的数据";
			infolog.info("ProductGroupService|delProduct");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ProductGroupService|delProduct",e);
		}
		return result;
	}

}
